/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.chimos.property.compiler;

import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.chimos.property.compiler.passone.PassOneFileVisitor;
import de.chimos.property.compiler.passtwo.javafx.PassTwoJFXFileVisitor;

/**
 * @author Niklas Hofmann
 */
public class CompilerTask {
    
    public static void run(File directory, Config config) throws Exception
    {
        if(directory == null || directory.exists() == false || directory.isDirectory() == false)
        {
            throw new Exception("Directory not found");
        }
        
        if(config.protocol != Config.Protocol.JAVAFX)
        {
            throw new Exception("No implementation for the requested protocol available");
        }
        
        System.out.println("Directory: " + directory.getAbsolutePath());
        
        System.out.println("Pass one");
        
        List<PropertyInfo> propertyList = new ArrayList<>();

        Files.walkFileTree(directory.toPath(), new PassOneFileVisitor(propertyList, config));
        
        System.out.println("Pass two");
        
        Map<String, Map<String, PropertyInfo>> propertyMap = new HashMap<>();
        
        for(PropertyInfo property : propertyList)
        {
            if(propertyMap.containsKey(property.className) == false)
            {
                propertyMap.put(property.className, new HashMap<String, PropertyInfo>());
            }
            
            propertyMap.get(property.className).put(property.propertyName, property);
        }
        
        Files.walkFileTree(directory.toPath(), new PassTwoJFXFileVisitor(propertyMap, config));
        
        System.out.println("Done");
    }
    
}
